//
//  payChargeTypeVC.m
//  ManageMoney
//
//  Created by 大明 on 16/4/28.
//  Copyright © 2016年 db. All rights reserved.
//

#import "payChargeTypeVC.h"
#import "PrepaidPhoneHomePageVC.h"
@interface payChargeTypeVC ()
{
    NSString *VerificationCodeStr;
    NSTimer *countDownTimer;
    NSInteger secondsCountDown; //60秒倒数计时
}
@property (weak, nonatomic) IBOutlet UILabel *cardIdLabel;/**< 信用卡号*/
@property (weak, nonatomic) IBOutlet UITextField *creditCardTextfield;/**< 信用卡背面3位数*/
@property (weak, nonatomic) IBOutlet UITextField *periodOfValidityTextfield;/**< 有效期*/
@property (weak, nonatomic) IBOutlet UITextField *mobileTextfield;/**< 手机号*/
@property (weak, nonatomic) IBOutlet UITextField *moneyTextfield;/**< 金额*/
@property (weak, nonatomic) IBOutlet UITextField *VerificationCodeTextfield;/**< 验证码*/
@property (weak, nonatomic) IBOutlet UIButton *VerificationCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *VerificationCodeBtn1;

@end

@implementation payChargeTypeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",_payChargeTypeDic);
//    self.cardIdLabel.text = _payChargeTypeDic[@"channelSummary"];
    self.cardIdLabel.text = _payChargeTypeStr;
}


//验证码按钮
- (IBAction)VerificationCodeBtn:(id)sender {
    if ([_creditCardTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"信用卡背面3位数不能为空"];
        return;
    }
    if ([_periodOfValidityTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"信用卡有效期不能为空"];
        return;
    }
    if ([_mobileTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"手机号不能为空"];
        return;
    }
    if ([_moneyTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"金额不能为空"];
        return;
    }
    
    NSString *retrieveClickUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"fund/recharge.do"];
    [SVProgressHUD show];
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"customerNo":CheckNull(UserInfo[@"NUMBER"]),@"channelNo":_payChargeTypeDic[@"channelNum"],@"tradeAmt":_moneyTextfield.text,@"cardNo":self.cardIdLabel.text,@"owner":@"",@"expireDate":_periodOfValidityTextfield.text,@"phone":_mobileTextfield.text,@"certNo":@"",@"cvv2":_creditCardTextfield.text};
    
    
    [HWHttpTool post:retrieveClickUrl params:pramas success:^(id json) {
        NSLog(@"%@",json);
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {

            //禁用短信发送按钮，一分钟后再次开启
            secondsCountDown = 60;//60秒倒计时
            countDownTimer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            VerificationCodeStr = dic[@"responseObj"][@"order_no"];
            
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误：%@",error);
        [SVProgressHUD dismiss];
    }];

}
//重复验证码
- (IBAction)VerificationCodeBtn1:(id)sender {
    if ([_creditCardTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"信用卡背面3位数不能为空"];
        return;
    }
    if ([_periodOfValidityTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"信用卡有效期不能为空"];
        return;
    }
    if ([_mobileTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"手机号不能为空"];
        return;
    }
    if ([_moneyTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"金额不能为空"];
        return;
    }
    NSString *retrieveClickUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"fund/resendMsg.do"];
    [SVProgressHUD show];
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"orderNo":VerificationCodeStr};
    
    
    [HWHttpTool post:retrieveClickUrl params:pramas success:^(id json) {
        NSLog(@"%@",json);
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            [SVProgressHUD showSuccessWithStatus:dic[@"responseMessage"]];

            //禁用短信发送按钮，一分钟后再次开启
            secondsCountDown = 60;//60秒倒计时
            countDownTimer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod1) userInfo:nil repeats:YES];
            
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误：%@",error);
        [SVProgressHUD dismiss];
    }];

}

-(void)timeFireMethod{
    secondsCountDown--;
    if(secondsCountDown<0){
        [countDownTimer invalidate];
        _VerificationCodeBtn.hidden = YES;
        _VerificationCodeBtn1.hidden = NO;
        [_VerificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_VerificationCodeBtn setBackgroundColor:[UIColor blueColor]];
        [_VerificationCodeBtn setEnabled:YES];
    }else{
        [_VerificationCodeBtn setEnabled:NO];
        [_VerificationCodeBtn setBackgroundColor:[UIColor lightGrayColor]];
        if([SVProgressHUD getSystemVersion ]>=8){
            [_VerificationCodeBtn setTitle:[NSString stringWithFormat:@"%ld秒后再获取",secondsCountDown] forState:UIControlStateNormal];
        }else{
            [_VerificationCodeBtn setTitle:@"请稍后片刻……" forState:UIControlStateNormal];
        }
    }
}


-(void)timeFireMethod1{
    secondsCountDown--;
    if(secondsCountDown<0){
        [countDownTimer invalidate];
        [_VerificationCodeBtn1 setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_VerificationCodeBtn1 setBackgroundColor:[UIColor blueColor]];
        [_VerificationCodeBtn1 setEnabled:YES];
    }else{
        [_VerificationCodeBtn1 setEnabled:NO];
        [_VerificationCodeBtn1 setBackgroundColor:[UIColor lightGrayColor]];
        if([SVProgressHUD getSystemVersion ]>=8){
            [_VerificationCodeBtn1 setTitle:[NSString stringWithFormat:@"%ld秒后再获取",secondsCountDown] forState:UIControlStateNormal];
        }else{
            [_VerificationCodeBtn1 setTitle:@"请稍后片刻……" forState:UIControlStateNormal];
        }
    }
}


//确认支付
- (IBAction)ConfirmThePaymentClick:(id)sender {
    if ([_VerificationCodeTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showSuccessWithStatus:@"验证码不能为空"];
        return;
    }
    
    NSString *retrieveClickUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"fund/paymentConfirm.do"];
    [SVProgressHUD show];
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"orderNo":VerificationCodeStr,@"checkCode":_VerificationCodeTextfield.text};
    
    
    [HWHttpTool post:retrieveClickUrl params:pramas success:^(id json) {
        NSLog(@"%@",json);
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            PrepaidPhoneHomePageVC *ctr = [[PrepaidPhoneHomePageVC alloc] initWithNibName:@"PrepaidPhoneHomePageVC" bundle:nil];
            [self.navigationController pushViewController:ctr animated:YES];
            
            
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误：%@",error);
        [SVProgressHUD dismiss];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
